package ShareLife.gar;


import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;

public class ToolOld {

	public static void drawImage(Canvas canvas, Bitmap blt, int x, int y, int w, int h, int bx, int by) {
		Rect src = new Rect();
		Rect dst = new Rect();

		src.left = bx;
		src.top = by;
		src.right = bx + w;
		src.bottom = by + h;

		dst.left = x;
		dst.top = y;
		dst.right = x + w;
		dst.bottom = y + h;
		canvas.drawBitmap(blt, src, dst, null);

		src = null;
		dst = null;
	}

	public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y) {
		canvas.drawBitmap(bitmap, x, y, null);
	}

	public static void flipZ(int[] dst, int[] src, int depth, int pos, boolean direction, int srcW, int srcH) {
		int startX, startY;
		int scaledWidth, scaledHeight;
		int posX, posY;
		int counterX, counterY;
		int halfSrcWidth;
		int srcWidth = srcW;
		int srcHeight = srcH;
		int dstWidth = srcW;
		int dstHeight = srcH + (depth << 1);
		halfSrcWidth = srcWidth >> 1;
		startX = halfSrcWidth - pos;
		int startOffsetY = startX * depth / halfSrcWidth;
		int doubleStartOffsetY = startOffsetY << 1;

		scaledWidth = pos << 1;
		counterX = 0;
		int offsetYCounter = 0;
		startY = direction ? depth + startOffsetY : depth - startOffsetY;
		posX = 0;
		int x = 0;
		int y;
		int temp;

		for (x = 0; x < scaledWidth; ++x) {

			counterY = 0;
			posY = 0;
			temp = startY * dstWidth + startX + x;
			scaledHeight = dstHeight - (startY << 1);
			for (y = 0; y < scaledHeight; ++y) {
				counterY += srcHeight;
				dst[temp + y * dstWidth] = src[posY * srcWidth + posX];
				posY = (counterY / scaledHeight);
			}
			offsetYCounter += doubleStartOffsetY;
			if (offsetYCounter >= scaledWidth) {
				offsetYCounter -= scaledWidth;
				startY = direction ? --startY : ++startY;
			}
			counterX += srcWidth;
			posX = (counterX / scaledWidth);
		}

	}

	public static byte[] int2byte(int[] src) {
		int srcLength = src.length;
		byte[] dst = new byte[srcLength << 2];

		for (int i = 0; i < srcLength; i++) {
			int x = src[i];
			int j = i << 2;
			dst[j++] = (byte) ((x >> 24) & 0xff);
			dst[j++] = (byte) ((x >> 16) & 0xff);
			dst[j++] = (byte) ((x >> 8) & 0xff);
			dst[j] = (byte) ((x >> 0) & 0xff);
		}
		return dst;
	}

	public static int[] byte2int(byte[] src) {
		int srcLength = src.length;
		int[] dst = new int[srcLength >> 2];
		int templength = dst.length;
		for (int i = 0; i < templength; i++) {
			int j = i << 2;
			dst[i] = ((src[j++] + 256) << 24) | (((src[j++] + 256) << 16)) | ((src[j++] + 256) << 8) | (src[j] + 256)
					& 0xff;
		}
		return dst;
	}

	public static void sendByteArray(DataOutputStream dos, byte[] sendData) {
		int currentIndex = 0;
		int storebytenumber = 0;
		while (currentIndex != sendData.length) {
			storebytenumber = sendData.length - currentIndex >= 512 ? 512 : sendData.length - currentIndex;
			byte temparray[] = new byte[storebytenumber];
			System.arraycopy(sendData, currentIndex, temparray, 0, storebytenumber);
			currentIndex += storebytenumber;
			try {
				dos.write(temparray, 0, storebytenumber);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	public static void receiveByteArray(DataInputStream dis, byte Data[]) {

		int currentIndex = 0;
		int storebytenumber = 0;
		while (currentIndex != Data.length) {
			storebytenumber = Data.length - currentIndex >= 512 ? 512 : Data.length - currentIndex;
			byte temparray[] = new byte[storebytenumber];
			try {
				dis.read(temparray, 0, storebytenumber);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.arraycopy(temparray, 0, Data, currentIndex, storebytenumber);
			currentIndex += storebytenumber;

		}

	}

	public static long formatPhoneNumber(String phoneNumberStr) {
		int beginIndex = 0;
		int endIndex = 0;
		String numStr = new String();

		while ((beginIndex = phoneNumberStr.indexOf("-", endIndex)) != -1) {
			numStr += phoneNumberStr.substring(endIndex, beginIndex);
			endIndex = beginIndex + 1;
		}
		numStr += phoneNumberStr.substring(endIndex, phoneNumberStr.length());
		return Long.parseLong(numStr);
	}

	public static void SaveContact(ContentResolver resolver, FriendItem aPeason) {
		ContentValues values = new ContentValues();
		long rawContactId = 0;
		Uri rawContactUri = null;
		try {
			rawContactUri = resolver.insert(RawContacts.CONTENT_URI, values);
			rawContactId = ContentUris.parseId(rawContactUri);
		} catch (Exception e) {
			
		}

		values.put(Data.RAW_CONTACT_ID, rawContactId);
		values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		values.put(StructuredName.DISPLAY_NAME, aPeason.name);
		resolver.insert(ContactsContract.Data.CONTENT_URI, values);

		Uri mobileUri = Uri.withAppendedPath(rawContactUri, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
		values.clear();
		values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
		values.put(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY, 1);
		values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
		values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, aPeason.phonephonenumber);
		resolver.insert(mobileUri, values);

		// Uri PhotoUri = Uri.withAppendedPath(rawContactUri,
		// ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
		values.clear();
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		aPeason.photoImage.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
		int photoRow = -1;
		String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + rawContactId + " AND "
				+ ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
				+ "'";
		Cursor cursor = resolver.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
		int idIdx = cursor.getColumnIndexOrThrow(BaseColumns._ID);
		if (cursor.moveToFirst()) {
			photoRow = cursor.getInt(idIdx);
		}
		cursor.close();
		values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
		values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
		values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes.toByteArray());
		values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);

		if (photoRow >= 0) {
			resolver.update(ContactsContract.Data.CONTENT_URI, values, BaseColumns._ID + " = " + photoRow, null);
		} else {
			resolver.insert(ContactsContract.Data.CONTENT_URI, values);
		}
	}
}
